﻿using System;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;

namespace Mattress.Engine.Matlab {
    [SuppressUnmanagedCodeSecurity()]
    internal static class engine {
        [DllImport("libeng.dll", EntryPoint = "engOpenSingleUse", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static SafeEnginePtr engOpenSingleUse(string startcmd, IntPtr reserved, out int retstatus);

        [DllImport("libeng.dll", EntryPoint = "engGetArray", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static SafeArrayPtr engGetArray(SafeEnginePtr engine, string name);

        [DllImport("libeng.dll", EntryPoint = "engPutArray", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engPutArray(SafeEnginePtr engine, SafeArrayPtr mp);

        [DllImport("libeng.dll", EntryPoint = "engEvalString", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engEvalString(SafeEnginePtr engine, string cmdstring);

        [DllImport("libeng.dll", EntryPoint = "engOutputBuffer", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engOutputBuffer(SafeEnginePtr engine, char[] buffer, int buflen);

        [DllImport("libeng.dll", EntryPoint = "engClose", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engClose(IntPtr engine);

        [DllImport("libeng.dll", EntryPoint = "engGetVariable", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static SafeArrayPtr engGetVariable(SafeEnginePtr engine, string variableName);

        [DllImport("libeng.dll", EntryPoint = "engPutVariable", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engPutVariable(SafeEnginePtr engine, string variableName, SafeArrayPtr ap);

        [DllImport("libeng.dll", EntryPoint = "engSetVisible", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engSetVisible(SafeEnginePtr engine, [MarshalAs(UnmanagedType.U1)]bool newVal);

        [DllImport("libeng.dll", EntryPoint = "engGetVisible", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        [SuppressUnmanagedCodeSecurity]
        public extern static int engGetVisible(SafeEnginePtr engine, [MarshalAs(UnmanagedType.U1)]out bool val);

    }
}
